﻿using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;
using System.Data.OleDb;

namespace Web.Pages.QuShui.DiXiaS._7.ChaXunGL
{
    public partial class QuShuiKXXCX : BasePage
    {
        #region 属性

        /// <summary>
        /// 保存结果集1
        /// </summary>
        private DataTable dtShenJingXX
        {
            get
            {
                return ViewState["dtShenJingXX"] as DataTable;
            }
            set
            {
                ViewState["dtShenJingXX"] = value;
            }
        }
      
        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();
        /// <summary>
        /// 行政区
        /// </summary>
        QS_JX_XINGZHENGQBLL XZQ = new QS_JX_XINGZHENGQBLL();
        /// <summary>
        /// 行业分类
        /// </summary>
        QS_JX_HANGYEFLXXBLL HYFL = new QS_JX_HANGYEFLXXBLL();
        /// <summary>
        /// 取水用途
        /// </summary>
        QS_JX_QUSHUIXXBLL QSYT = new QS_JX_QUSHUIXXBLL();
        /// <summary>
        /// 管理部门
        /// </summary>
        QS_JX_BUMENXXBLL GLBM = new QS_JX_BUMENXXBLL();
        /// <summary>
        /// 流域信息
        /// </summary>
        QS_JX_LIUYUXXBLL LYXX = new QS_JX_LIUYUXXBLL();
        /// <summary>
        /// 水利分片
        /// </summary>
        QS_JX_SHUILIFPXXBLL SLFP = new QS_JX_SHUILIFPXXBLL();
        /// <summary>
        /// 水资源分区
        /// </summary>
        QS_JX_SHUIZIYFQBLL SZYFQ = new QS_JX_SHUIZIYFQBLL();
        /// <summary>
        /// 井标记
        /// </summary>
        QS_JX_JINGBIAOJXXBLL JBJ = new QS_JX_JINGBIAOJXXBLL();
        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
            }

            gvShenJingXX.DataSource = dtShenJingXX;
            gvShenJingXX.DataBind();
        }




        /// <summary>
        /// 查询信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChaXun_Click(object sender, EventArgs e)
        {

            LoadGridData();
            if (this.dtShenJingXX == null || this.dtShenJingXX.Rows.Count < 1)
            {
                //txtYongHuH2.Text = "";
                //txtYongHuM2.Text = "";
                //txtHuanNeiHW.Text = "";
                //txJingBianH.Text = "";
                //txtDiZhi.Text = "";
                //txtXingZhengQ.Text = "";
                //txtJingLeiX.Text = "";
                //txtJingXingZ.Text = "";
                //txtJingBiaoJ.Text = "";
                //txtLiuYu.Text = "";
                //txtYeWuY.Text = "";
                //txtGuanLiBM.Text = "";
                //txtShuiZiYFQ.Text = "";
                //txtCengCi.Text = "";
                //txtShenDu.Text = "";
                //txtQuShuiYT.Text = "";
                //txtShuiLiFP.Text = "";
                //txtKouJing.Text = "";
                //txtHangYeFL.Text = "";
                //memoBeiZhu.Text = "";
                //txtYuanJingH.Text = "";
                //txtShuiBiaoSL.Text = "";
                //txtXuKeZ.Text = "";
                ////txtBenYueSL.Text = "";
                //zjl.Text = "共0条";
            }
        }



       


        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            int wtype = BusinessTool.WType;
            gvShenJingXX.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //水量年月
            string water_y_m = BusinessTool.GetSLYMorZWNY(1, wtype).ToString();

            int userid = LoginInfo.CurrentUser.UserID;//当前登陆人
            if (userid == 116 || userid == 99999 || userid == 113 || userid == 111)
            {
                btnXiuGai.Visible = true;
            }

            //加载管理部门
            LoadGLBM();
            //加载行政区
            LoadXZQ();
            //加载业务员
            LoadYWY();
            //加载取水用途
            LoadQSYT();
            //加载行业分类
            LoadHYFL();
            //加载井标记
            LoadLY();
        }

        /// <summary>
        /// 加载业务员
        /// </summary>
        private void LoadYWY()
        {
            PageTools.QSLoadCombox(cmbYWY, PageTools.eLoadType.业务员QS, null, "");
        }

        /// <summary>
        /// 加载管理部门
        /// </summary>
        private void LoadGLBM()
        {
            DataTable dt = GLBM.SelectQS_JX_BUMENXXesDynamic("I_JLZT=0 and s_bumenbh not in('0','600')", null);
            cbmGuanLiBM.Items.Insert(0, new ListEditItem("", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbmGuanLiBM.Items.Add(dt.Rows[i]["S_BUMENMC"].ToString(), dt.Rows[i]["S_BUMENBH"]);
            }
            for (int i = DateTime.Now.Year - 10; i <= DateTime.Now.Year ; i++)
            {
                cmbYear.Items.Add(i.ToString(), i.ToString());
            }

            for (int i = 1; i <= 12; i++)
            {
                cmbMonth.Items.Add(i.ToString(), i.ToString());
            }
            cmbYear.Value = BusinessTool.GetSLYMorZWNY(1, BusinessTool.WType) / 100;
            cmbMonth.Value = BusinessTool.GetSLYMorZWNY(1, BusinessTool.WType) % 100;




            QS_JX_SHUILIFPXXBLL fpbll = new QS_JX_SHUILIFPXXBLL();
            QS_JX_SHUIZIYFQBLL szybll = new QS_JX_SHUIZIYFQBLL();

            DataTable fpdt = fpbll.SelectQS_JX_SHUILIFPXXesDynamic("  i_jlzt=0 ", "");

            DataTable szydt = szybll.SelectQS_JX_SHUIZIYFQsAll();

            cmbShuiZiY.DataSource = szydt;
            cmbShuiZiY.TextField = "S_SHUIZIYMC";
            cmbShuiZiY.ValueField = "S_SHUIZIYBH";
            cmbShuiZiY.DataBind();
            cmbShuiZiY.Items.Insert(0, new ListEditItem("", ""));

            cmbShuiLiFP.DataSource = fpdt;
            cmbShuiLiFP.TextField = "S_SHUILIFPMC";
            cmbShuiLiFP.ValueField = "S_SHUILIFPBH";
            cmbShuiLiFP.DataBind();
            cmbShuiLiFP.Items.Insert(0, new ListEditItem("", ""));
            DataTable dtly = LYXX.SelectQS_JX_LIUYUXXesDynamic("I_JLZT=0", null);
            for (int i = 0; i < dtly.Rows.Count; i++)
            {
                cmbLiuYu.Items.Add(dtly.Rows[i]["S_LIUYUMC"].ToString(), dtly.Rows[i]["S_LIUYUBH"]);
            }
            cmbLiuYu.Items.Insert(0, new ListEditItem("", ""));
        }

        /// <summary>
        /// 加载行政区
        /// </summary>
        private void LoadXZQ()
        {
            DataTable dt = XZQ.SelectQS_JX_XINGZHENGQsDynamic("I_JLZT=0", null);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbXingZhengQ.Items.Add(dt.Rows[i]["S_XINGZHENGQM"].ToString(), dt.Rows[i]["S_XINGZHENGQH"]);
            }
        }




        /// <summary>
        /// 加载 取水用途
        /// </summary>
        private void LoadQSYT()
        {
            DataTable dt = QSYT.SelectQS_JX_QUSHUIXXesDynamic("I_JLZT=0", null);
            cmbQuShuiYT.Items.Insert(0, new ListEditItem("", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbQuShuiYT.Items.Add(dt.Rows[i]["S_QUSHUIMC"].ToString(), dt.Rows[i]["S_QUSHUIBH"]);
            }
        }
        /// <summary>
        /// 加载行业分类
        /// </summary>
        private void LoadHYFL()
        {
            DataTable dt = HYFL.SelectQS_JX_HANGYEFLXXesDynamic("I_JLZT=0", null);
            cmbHangYeLX.Items.Insert(0, new ListEditItem("", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbHangYeLX.Items.Add(dt.Rows[i]["S_FENLEIMC"].ToString(), dt.Rows[i]["S_FENLEIBH"]);
            }
        }

        /// <summary>
        /// 加载井标记
        /// </summary>
        private void LoadLY()
        {


        }

        /// <summary>
        /// 加载查询数据
        /// </summary>
        private void LoadGridData()
        {
            try
            {
                BindShenJingXX();
            }
            catch (Exception ex)
            { 
                
            }
                  


        }
       
       
        private void BindShenJingXX()
        {
            int ym = Convert.ToInt32(cmbYear.Value) * 100 + Convert.ToInt32(cmbMonth.Value);
            int y = Convert.ToInt32(cmbYear.Value);
            int m = Convert.ToInt32(cmbMonth.Value);
            int wtype = BusinessTool.WType;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if (txtYongHuH.Text.Trim().Length > 0)
            {
                sb.AppendFormat("  and  B.S_YONGHUH ='{0}' ", txtYongHuH.Text.Trim());
            }
            if (txtYongHuM.Text.Trim().Length > 0)
            {
                sb.AppendFormat("  and  B.S_YONGHUM  like '%{0}%' ", txtYongHuM.Text.Trim());
            }
            if (txtYongHuDZ.Text.Trim().Length > 0)
            {
                sb.AppendFormat("  and  B.S_DIZHI  like '%{0}%' ", txtYongHuDZ.Text.Trim());
            }
            if (txtJingBianH.Text.Trim().Length > 0)
            {
                sb.AppendFormat("  and  A.S_QUSHUIKBH like '%{0}%' ", txtJingBianH.Text.Trim());
            }
            if (txtXuKeZH.Text.Trim().Length > 0)
            {
                sb.AppendFormat("  and  A.S_XUKEZBH like '%{0}%' ", txtXuKeZH.Text.Trim());
            }
            if (txtQuShuiDZ.Text.Trim().Length > 0)
            {
                sb.AppendFormat("  and  A.S_QUSHUIDZ like '%{0}%' ", txtQuShuiDZ.Text.Trim());
            }
            if (cmbQuShuiYT.Value != null && cmbQuShuiYT.Value.ToString().Trim().Length > 0)
            {
                sb.AppendFormat("  and  B.S_QUSHUIYTBH = '{0}' ", cmbQuShuiYT.Value);
            }
            if (cmbHangYeLX.Value != null && cmbHangYeLX.Value.ToString().Trim().Length > 0)
            {
                sb.AppendFormat("  and  B.S_HANGYEBH = '{0}' ", cmbHangYeLX.Value);
            }
            if (cbmGuanLiBM.Value != null && cbmGuanLiBM.Value.ToString().Trim().Length > 0)
            {
                sb.AppendFormat("  and  B.S_BUMENBH = '{0}' ", cbmGuanLiBM.Value);
            }
            if (cmbLiuYu.Value != null && cmbLiuYu.Value.ToString().Trim().Length > 0 && Convert.ToInt32(cmbLiuYu.Value) > -1)
            {
                sb.AppendFormat("  and  B.S_LIUYUBH = '{0}' ", cmbLiuYu.Value);
            }
            if (cmbShuiZiY.Value != null && cmbShuiZiY.Value.ToString().Trim().Length > 0)
            {
                sb.AppendFormat("  and  B.S_SHUIZIYBH = '{0}' ", cmbShuiZiY.Value);
            }
            if (cmbShuiLiFP.Value != null && cmbShuiLiFP.Value.ToString().Trim().Length > 0)
            {
                sb.AppendFormat("  and  B.S_FENPIANBH = '{0}' ", cmbShuiZiY.Value);
            }
            //if (cmbShenJingSFTJ.Value != null && cmbShenJingSFTJ.Value.ToString().Trim().Length > 0 && txtShenJingSF.Text.Trim().Length>0)
            //{
            //    sb.AppendFormat(" A.S_FENPIANBH = '{0}' ", cmbShuiZiY.Value);
            //}

            if (cmbHuanNeiHW.Value != null && cmbHuanNeiHW.Value.ToString().Trim().Length > 0)
            {
                sb.AppendFormat("  and  B.S_HUANXIANBJ = '{0}' ", cmbHuanNeiHW.Value);
            }
            if (cmbXingZhengQ.Value != null && cmbXingZhengQ.Value.ToString().Trim().Length > 0)
            {
                sb.AppendFormat("  and  B.S_XINGZHENGQBH = '{0}' ", cmbXingZhengQ.Value);
            }

            if (cmbYWY.Value != null && cmbYWY.Value.ToString() != "-1")
            {
                sb.AppendFormat("  and  B.S_YEWUY = '{0}' ", cmbYWY.Value);
            }
            if (cmbLeiXing.Value != null && cmbLeiXing.Value.ToString().Trim().Length > 0)
            {
                sb.AppendFormat("  and  A.S_QUSHUILX = {0} ", cmbLeiXing.Value);
            }
            if (cmbQSFS.Value != null && cmbQSFS.Value.ToString().Trim().Length > 0)
            {
                sb.AppendFormat("  and  A.S_QUSHUIFS = {0} ", cmbQSFS.Value);
            }
            if (cmbQSSY.Value != null && cmbQSSY.Value.ToString().Trim().Length > 0)
            {
                sb.AppendFormat("  and  A.S_QUSHUISY = {0} ", cmbQSSY.Value);
            }



            DataSet ds = _SpHelper.P_QS_CX_QUSHUIKXXCX( y, m, sb.ToString());

            dtShenJingXX = ds.Tables[0];
            gvShenJingXX.DataSource = dtShenJingXX;
            gvShenJingXX.DataBind();
            zjl.Text = "共" + gvShenJingXX.VisibleRowCount + "条";
            gvShenJingXX.FocusedRowIndex = -1;
            gvShenJingXX.FocusedRowIndex = 0;
        }

        /// <summary>
        /// 前台表格行改变事件回调方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            int id = Convert.ToInt32(this.hdCollection.Get("ID"));
            if (e.Parameter == "setinfo")
            {
                string msg = "";
                if (this.dtShenJingXX != null)
                {
                    int y = Convert.ToInt32(cmbYear.Value);
                    int m = Convert.ToInt32(cmbMonth.Value);
                    string where = "";

                    where = " and A.ID='" + id + "'";

                    DataTable dt_temp = _SpHelper.P_QS_CX_QUSHUIKXXCX(y, m, where).Tables[0];

                    msg = string.Join("|",
                        dt_temp.Rows[0]["用户号"].ToString(),
                        dt_temp.Rows[0]["用户名"].ToString(),
                         dt_temp.Rows[0]["环内环外"].ToString(),
                        dt_temp.Rows[0]["取水口号"].ToString(),
                        dt_temp.Rows[0]["用户地址"].ToString(),
                         dt_temp.Rows[0]["行政区名"].ToString(),
                        dt_temp.Rows[0]["取水类型"].ToString(),
                         dt_temp.Rows[0]["取水地点"].ToString(),
                         dt_temp.Rows[0]["流域"].ToString(),
                         "",
                        //dt_temp.Rows[0]["取水方式"].ToString(),
                        dt_temp.Rows[0]["取水水源"].ToString(),
                        dt_temp.Rows[0]["退水位置"].ToString(),
                         dt_temp.Rows[0]["取水口牌号"].ToString(),
                        dt_temp.Rows[0]["取水口名"].ToString(),
                        dt_temp.Rows[0]["取水用途"].ToString(),
                        dt_temp.Rows[0]["业务员名"].ToString(),
                        dt_temp.Rows[0]["主管部门名称"].ToString(),
                        dt_temp.Rows[0]["水资源分区"].ToString(),
                        dt_temp.Rows[0]["行业分类"].ToString(),
                        dt_temp.Rows[0]["水利分片"].ToString(),
                         dt_temp.Rows[0]["表数"].ToString(),
                         "",
                        //dt_temp.Rows[0]["本月水量"].ToString(),
                        dt_temp.Rows[0]["备注"].ToString());
                }

                e.Result = e.Parameter + "|0|" + msg;
            }
        }
        #endregion

        protected void btnExport_Click(object sender, EventArgs e)
        {
            string sName = "";
            ASPxGridViewExporter1.GridViewID = "gvShenJingXX";
            sName = "取水口信息";
            gvShenJingXX.DataSource = dtShenJingXX;
            gvShenJingXX.DataBind();
            gvShenJingXX.Settings.ShowFooter = false;
            ASPxGridViewExporter1.WriteXlsToResponse(sName);
        }


       
    }
}